//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.ComponentModel;
using OpenLS.Spreadsheet.Pivot;

namespace OpenLS.Spreadsheet
{
    partial class Cell
    {
        internal Pivot.PivotTableDefinition PivotTable
        {
            get
            {
                if (!HasExtraData)
                {
                    if (!IsVolatile)
                        return null;
                    if (this.Worksheet.PivotTables.Count == 0) //\\ leave optimization!!
                        return null;
                    foreach (PivotTableDefinition table in Worksheet.PivotTables)
                        if (table.Contains(this))
                            return table;
                    return null;
                }
                return EnsureExtraData.TableBase as PivotTableDefinition;
            }
            set
            {
                if (value == null)
                {
                    if (HasExtraData)
                    {
                        if (ExtraData.TableBase != null)
                            UnregisterTableEvents(ExtraData.TableBase);
                        ExtraData.TableBase = value;
                        ForgetExtraData();
                        NotifyDifferentialStyleRecordChanged();
                    }
                }
                else
                {
                    if (EnsureExtraData.TableBase == value)
                        return;
                    if (ExtraData.TableBase != null)
                        UnregisterTableEvents(ExtraData.TableBase);
                    EnsureExtraData.TableBase = value;
                    RegisterTableEvents(ExtraData.TableBase);
                    NotifyDifferentialStyleRecordChanged();
                }
            }
        }

        internal Table Table
        {
            get
            {
                if (!HasExtraData)
                {
                    if (!IsVolatile)
                        return null;
                    if (this.Worksheet.Tables.Count == 0) //\\ leave optimization!!
                        return null;
                    foreach (Table table in Worksheet.Tables)
                        if (table.Range.Contains(this))
                            return table;
                    return null;
                }
                return EnsureExtraData.TableBase as Table;
            }
            set
            {
                if (value == null)
                {
                    if (HasExtraData)
                    {
                        if (ExtraData.TableBase != null)
                            UnregisterTableEvents(ExtraData.TableBase);
                        ExtraData.TableBase = value;
                        ForgetExtraData();
                        NotifyDifferentialStyleRecordChanged();
                    }
                }
                else
                {
                    if (EnsureExtraData.TableBase == value)
                        return;
                    if (ExtraData.TableBase != null)
                        UnregisterTableEvents(ExtraData.TableBase);
                    EnsureExtraData.TableBase = value;
                    RegisterTableEvents(ExtraData.TableBase);
                    NotifyDifferentialStyleRecordChanged();
                }
            }
        }

        internal TableBase TableBase
        {
            get { var table = this.Table;
            if (table != null)
                return table;
                return PivotTable;
            }
            set {
                if (value is PivotTableDefinition)
                    PivotTable = (PivotTableDefinition) value;
                else
                {
                    this.Table = (Table) value;
                }
            }
        }


        private void RegisterTableEvents(INotifyPropertyChanged table)
        {
            table.PropertyChanged += table_PropertyChanged;
        }

        void table_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Style")
                NotifyDifferentialStyleRecordChanged();
        }

        private void UnregisterTableEvents(INotifyPropertyChanged table)
        {
            table.PropertyChanged-=table_PropertyChanged;
        }

    }
}
